﻿using System;
using System.Text.RegularExpressions;
using System.Data;
using System.Diagnostics;
using System.Collections.Specialized;
using System.Text;
using System.Resources; 

namespace MinConsole.Gaf.Utils.Log
{
    class Logger
    {

        /// <summary>
        /// Logs a message to the predifiend in the app settings logdir 
        /// </summary>
        /// <param name="msg"></param>
        public static void Log(string msg)
        {
            #region Try 
            try
            {
            
                //if have to log to the console log
                if (MinConsole.Conf.AppInstSettings.Instance.LogToConsole == true)
                {
                    MinConsole.Gaf.Utils.Log.Logger.LogToConsole(msg);
                } //eof if


                //if have to log to a file log
                if (MinConsole.Conf.AppInstSettings.Instance.LogToFile == true)
                {
                    MinConsole.Gaf.Utils.Log.Logger.DebugToFile(msg);
                    MinConsole.Gaf.Utils.Log.Logger.LogToFile(msg);
                }
            
            } //eof try 
            #endregion Try 

            #region Catch 

            catch (Exception ex)
            {
                Console.WriteLine("Failed to log msg:" + msg.ToString());
                Console.WriteLine("Gaf.Utils.Log.Logger.Debug catch" + ex.Message.ToString());
            } //eof catch 
            
            #endregion Catch

        } //eof method 

        /// <summary>
        /// Logs a message to the predifiend in the app settings logdir 
        /// </summary>
        /// <param name="msg"></param>
        public static void LogToFile(string msg)
        {
            //get the LogDir from the settings file
            String strLogDir = String.Empty;
            strLogDir = MinConsole.Conf.AppInstSettings.Instance.LogDir; 
            
            //add the \ at the end if it is not added 
            if(strLogDir.EndsWith(@"\") == false)
                strLogDir = strLogDir + @"\";

            try
            {
                //create the dir if it does not exist 
                if (System.IO.Directory.Exists(strLogDir) == false)
                    System.IO.Directory.CreateDirectory(strLogDir);
            }
            catch (Exception ex)
            {
                Console.WriteLine("The log directory " + strLogDir + " cannot be created " + ex.Message);
                return;
            }
            //build the LogFileName from the settings file
            String strLogFile = String.Empty;
            msg = DateTime.Now.ToString(MinConsole.Conf.AppInstSettings.Instance.LogMessageSuffix) + " " + msg;
            
                //todo: add generic file log name according to the application name ...
                strLogFile = strLogDir + DateTime.Now.ToString(
                MinConsole.Conf.AppInstSettings.Instance.LogFileNameTimePattern.ToString()) + "." + 
                MinConsole.Conf.AppInstSettings.Instance.LogFileNameSuffix.ToString();

               MinConsole.Gaf.IO.FileUtility.AppendToFile(strLogFile, msg);

        } //eof method 

        /// <summary>
        /// Logs a message to the predifiend in the app settings logdir 
        /// </summary>
        /// <param name="msg">the msg to log </param>
        public static void LogToConsole(string msg)
        {
            msg = DateTime.Now.ToString(MinConsole.Conf.AppInstSettings.Instance.LogMessageSuffix) + " " + msg;
            Console.WriteLine(msg); 
        } //eof method 

        /// <summary>
        /// Logs a message to the predifiend in the app settings logdir 
        /// </summary>
        /// <param name="msg">the msg to log </param>
        public static void LogErrorToConsole(string msg)
        {
            msg = DateTime.Now.ToString(MinConsole.Conf.AppInstSettings.Instance.LogMessageSuffix) + " " + msg;
            Console.Error.WriteLine(msg);
        } //eof method 


        /// <summary>
        /// Logs a message to the predifiend in the app settings logdir 
        /// </summary>
        /// <param name="msg"></param>
        public static void Debug(string msg)
        {
            #region Try
            try
            {

                //if have to log to the console log
                if (MinConsole.Conf.AppInstSettings.Instance.LogToConsole == true)
                {
                    MinConsole.Gaf.Utils.Log.Logger.DebugToConsole(msg);
                } //eof if


                //if have to log to a file log
                if (MinConsole.Conf.AppInstSettings.Instance.LogToFile == true)
                {
                    MinConsole.Gaf.Utils.Log.Logger.DebugToFile(msg);
                }


            } //eof try 
            #endregion Try
                
            #region Catch

            catch (Exception ex)
            {
                Console.Error.WriteLine("Failed to log msg:" + msg.ToString());
                Console.Error.WriteLine("Gaf.Utils.Log.Logger.Debug catch" + ex.Message.ToString());
            } //eof catch 

            #endregion Catch

        } //eof method 

        /// <summary>
        /// Logs a message to the predifiend in the app settings logdir 
        /// </summary>
        /// <param name="msg"></param>
        public static void DebugToFile(string msg)
        {
            //get the LogDir from the settings file
            String strLogDir = String.Empty;
            strLogDir = MinConsole.Conf.AppInstSettings.Instance.LogDir;

            //add the \ at the end if it is not added 
            if (strLogDir.EndsWith(@"\") == false)
                strLogDir = strLogDir + @"\";

            try
            {
                //create the dir if it does not exist 
                if (System.IO.Directory.Exists(strLogDir) == false)
                    System.IO.Directory.CreateDirectory(strLogDir);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("The log directory " + strLogDir + " cannot be created " + ex.Message);
                return;
            }
            //build the LogFileName from the settings file
            String strDebugFile = String.Empty;
            msg = DateTime.Now.ToString(MinConsole.Conf.AppInstSettings.Instance.LogMessageSuffix) + " "  + msg;

            //todo: add generic file log name according to the application name ...
            strDebugFile = strLogDir + DateTime.Now.ToString(
            MinConsole.Conf.AppInstSettings.Instance.LogFileNameTimePattern.ToString()) + "." +
            MinConsole.Conf.AppInstSettings.Instance.DebugFileNameSuffix.ToString();

            MinConsole.Gaf.IO.FileUtility.AppendToFile(strDebugFile, msg);

        } //eof method 

        /// <summary>
        /// Logs a message to the predifiend in the app settings logdir 
        /// </summary>
        /// <param name="msg">the msg to log </param>
        public static void DebugToConsole(string msg)
        {
            msg = DateTime.Now.ToString(MinConsole.Conf.AppInstSettings.Instance.LogMessageSuffix) + " "  + msg;
            Console.WriteLine(msg);
        } //eof method 
        






    } //eof class 
//eof class
} //eof namespace 
